package worldinruin.gui.imp.gameview.gameboard.tiles;


import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.util.ArrayList;

import javax.swing.Action;

import worldinruin.controllers.ControlFactory;
import worldinruin.controllers.ControlFactory.Controllers;
import worldinruin.core.IBuilding;
import worldinruin.core.ITile;
import worldinruin.core.ITroop;
import worldinruin.gui.imp.gameview.gameboard.BuildingPiece;
import worldinruin.gui.imp.gameview.gameboard.ITilePiece;
import worldinruin.gui.imp.gameview.gameboard.TroopPiece;
import worldinruin.resources.Components;
import worldinruin.resources.Events;
import worldinruin.resources.IconMap;
import worldinruin.resources.Panels;
import worldinruin.utils.ResizableButton;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class TileView extends ResizableButton implements ITileView, ITilePiece {		
	
	private ITile tileModel;
	private ArrayList<TroopPiece> troopPieces = new ArrayList<TroopPiece>();
	private BuildingPiece buildingPiece;
	private Panels mainPanelType = Panels.GAMEVIEWMAINPANEL;
	private Panels cardStack = Panels.GAMEVIEWLEFTCARDSTACK;
	private Panels subPanelType = Panels.EMPTYTILEINFOPANEL;
	private Action listener;
	
	/**
	 * Creates a tile on gameboard.
	 * @param tileModel Tile to display on gameboard.
	 * @param img Image for tile.
	 */
	public TileView(ITile tileModel, Image img){
		super(img);
		this.tileModel = tileModel; 
		tileModel.addPropertyChangeListener(this);
		initListener();
		setLayout(new FlowLayout());
		
	}

	/**
	 * Creates a tile on gameboard.
	 */
	public TileView() {
		super();
	}

	/**
	 * Initiate listeners to components.
	 */
	private void initListener() {
		listener = ControlFactory.getControl(Controllers.GAMEBOARD_CONTROLLER);
		
		this.addMouseListener((MouseListener) listener);
		listener.putValue(Components.tileViewButton, this);
	}
	
	/**
	 * Update view on tile.
	 */
	private void updateView() {
		removeAll();
		
		updateBuilding();
		updateTroops();
	}
	
	/**
	 * Update troops on tile.
	 */
	private void updateTroops() {
		for (TroopPiece tp : troopPieces) {
			tp.addMouseListener((MouseListener) listener);
			add(tp);
			listener.putValue(Components.troopViewButton, tp);
		}
	}
	
	/**
	 * Updates buildings on tile.
	 */
	private void updateBuilding() {
		if (tileModel.getBuilding() != null) {
			
			IBuilding building = tileModel.getBuilding();
			
			buildingPiece = new BuildingPiece(IconMap.getInstance().getIcon(building.getBuildingType()).getImage(), building);
			add(buildingPiece);
			
			buildingPiece.addMouseListener((MouseListener) listener);
			listener.putValue(Components.buildingViewButton, buildingPiece);
		}
	}
	
	
	
	@Override
	public void propertyChange(PropertyChangeEvent arg0) {
		
		if (arg0.getPropertyName().equals(Events.troopAdded)) {
			
			ArrayList<ITroop> troops = (ArrayList<ITroop>) arg0.getNewValue();
			
			troopPieces.clear();
			
			for (ITroop t : troops) {
				TroopPiece troopPiece = new TroopPiece(t);
				troopPieces.add(troopPiece);
			}
			
			updateView();
			
		}
		
		else if (arg0.getPropertyName().equals(Events.troopRemoved)) {

			ArrayList<ITroop> troops = (ArrayList<ITroop>) arg0.getNewValue();
			
			troopPieces.clear();
			
			for (ITroop t : troops) {
				TroopPiece troopPiece = new TroopPiece(t);
				troopPieces.add(troopPiece);
			}
			
			updateView();
		}
		
		else if (arg0.getPropertyName().equals(Events.buildingAdded)){
			updateView();
		}
		
		else if (arg0.getPropertyName().equals(Events.buildingRemoved)){
			updateView();
			
		}
		revalidate();
		repaint();
		
	}

	@Override
	public ITile getPieceModel() {
		return tileModel;
	}
	
	@Override
	public ITile getTileModel() {
		return tileModel;
	}

	@Override
	public Panels getMainPanelType() {
		return mainPanelType;
	}

	@Override
	public Panels getCardStack() {
		return cardStack;
	}

	@Override
	public Panels getSubPanelType() {
		return subPanelType;
	}
	
}
